package com.lianxian.xindong.life.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.entities.TopTitleModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FoodRankingFilterTwoAdapter extends RecyclerView.Adapter {
    List<TopTitleModel> list;
    Context context;
    public int currentPosition;

    public FoodRankingFilterTwoAdapter(List<TopTitleModel> list, Context context, int currentPosition) {
        this.list = list;
        this.context = context;
        this.currentPosition = currentPosition;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_food_ranking_filter_two, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TopTitleModel model = list.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.nameTV.setText(model.getName());
        if (currentPosition == position) {
            viewHolder.nameLayout.setBackgroundResource(R.drawable.filter_two_item_background);
        } else {
            viewHolder.nameLayout.setBackgroundResource(R.color.transparent);
        }
        viewHolder.nameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPosition = position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.filter_two_name)
        TextView nameTV;
        @BindView(R2.id.item_filter_two_layout)
        LinearLayout nameLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
